package com.minglead.springboot.common.config.webxmladapter;

import java.util.HashMap;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
/*import org.apache.shiro.cache.ehcache.EhCacheManager;*/
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.CharacterEncodingFilter;
import com.alibaba.druid.util.StringUtils;
import com.minglead.springboot.common.config.Global;
import com.minglead.springboot.common.config.webxmladapter.servlet.ValidateCodeServlet;

@Configuration
@ComponentScan
@EnableAutoConfiguration
@ImportResource(locations={"classpath:applicationContext.xml"})
@Order(Ordered.HIGHEST_PRECEDENCE)
public class WebAppRootContext implements ServletContextInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
/*    	servletContext.setInitParameter("contextConfigLocation", "classpath*:/applicationContext*.xml");
        servletContext.addListener(WebContextListener.class);*/
    } 
    /**
     * 为什么不使用@ServletComponentScan
     * 注解是因为注解的方式，是因为server.servlet.path项目名称不好注入
     * @return
     */
	@Bean
	public ServletRegistrationBean<ValidateCodeServlet> servletRegistrationBean() {
		String url = "/servlet/validateCodeServlet";
		String contextPath = Global.getContextPath();
		if(!StringUtils.isEmpty(contextPath))
			url = contextPath+url;
		return new ServletRegistrationBean<ValidateCodeServlet>(new ValidateCodeServlet(), url);
	}
       
       
    /**
     * 非自定义filter
     * 编码过滤
     * @return
     */
    @Bean
    public Filter characterEncodingFilter() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        return characterEncodingFilter;
    }
}